package com.drguo.higouguo.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.drguo.higouguo.dao.CategoryDao;
import com.drguo.higouguo.dao.GoodsDao;
import com.drguo.higouguo.entity.Goods;
import com.drguo.higouguo.entity.PageBean;
import com.drguo.higouguo.service.GoodsService;
import com.drguo.higouguo.utils.GuoUtil;

public class GoodsServiceImpl implements GoodsService{
	private GoodsDao goodsDao;
	private CategoryDao categoryDao;
	
	public GoodsDao getGoodsDao() {
		return goodsDao;
	}
	public void setGoodsDao(GoodsDao goodsDao) {
		this.goodsDao = goodsDao;
	}
	public CategoryDao getCategoryDao() {
		return categoryDao;
	}
	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}
	//增加商品
	@Override
	public void addGoods(Goods goods) {
		//商品ID（主键）
		goods.setId(GuoUtil.getId());
		goodsDao.addGoods(goods);
		//增加一类商品中所含商品的个数
		categoryDao.updateGoodsNum(goods.getCategoryId(), 1);
	}
	//商品列表
	@Override
	public List<Goods> getGoods(Goods goods) {
		
		return goodsDao.getGoods(goods);
	}
	//分页
	@Override
	public PageBean<Goods> getGoodsByPage(int page, Goods goods) {
		int pageSize=8;
		PageBean<Goods> pageBean = new PageBean<Goods>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("page", page);
		params.put("pageSize", pageSize);
		params.put("goods", goods);
		List<Goods> data =goodsDao.getGoodsByPage(params);
		
		pageBean.setData(data);
		pageBean.setPage(page);
		pageBean.setPageSize(pageSize);
		pageBean.setActualPageSize(data.size());
		int totalNum = goodsDao.getGoodsNum(goods);
		pageBean.setTotalNum(totalNum);
		//除不尽总页数要加一，因为10%3=3，三页只能放9条数据。
		int totalPage = totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;
		pageBean.setTotalPage(totalPage);
		return pageBean;
	}
	//商品修改页面
	@Override
	public Goods getGoodsById(String id) {
		
		return goodsDao.getGoodsById(id);
	}
	//商品修改
	@Override
	public void updateGoods(Goods goods) {
		Goods oldGoods = goodsDao.getGoodsById(goods.getId());
		oldGoods.setName(goods.getName());
		oldGoods.setGoodsNo(goods.getGoodsNo());
		oldGoods.setCategoryId(goods.getCategoryId());
		oldGoods.setStock(goods.getStock());
		oldGoods.setPrice1(goods.getPrice1());
		oldGoods.setPrice2(goods.getPrice2());
		oldGoods.setDescription(goods.getDescription());
		oldGoods.setThumbnail(goods.getThumbnail());
		goodsDao.updateGoods(oldGoods);
	}
	//删除商品
	@Override
	public void delGoods(String id) {
		goodsDao.delGoods(id);
	}
	//根据类别ID获取商品
	@Override
	public List<Goods> getGoodsByCateId(String cateId) {
		
		return goodsDao.getGoodsByCateId(cateId, 6);//一页6个商品
	}
	//为了购物车
	@Override
	public List<Goods> getGoodsesByIds(String... ids) {
		
		return goodsDao.getGoodsesByIds(ids);
	}
	

	
}
